#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "AlphaBeta.h"
#include "MTDF.h"
#include "othelloTranspositionNode.h"
using namespace std;

int main(int argc, char* argv[])
{

	double start =0.0;
	double finish =0.0;

	struct timeval t_p;

	if(argc == 3 || argc==4)
	{

		if(!strcmp(argv[1],"AB"))
		{
			int depth = atoi(argv[2]);
			state_t iNode;
			AlphaBeta instance;
            bool color = false;

			iNode = iNode.genInit(depth, color);

			if (!gettimeofday(&t_p,NULL))
				start = (double) t_p.tv_sec + ((double) t_p.tv_usec)/1000000.0;
			else{
					cout << "\nProblemas generando el tiempo final.\n";
					exit(EXIT_FAILURE);
			}

			int gv = instance.alphabetaMinimax(iNode, -INFINITY, INFINITY, color);

			if (!gettimeofday(&t_p,NULL))
				finish = (double) t_p.tv_sec + ((double) t_p.tv_usec)/1000000.0;
			else
				{
					cout << "\nProblemas generando el tiempo final.\n";
					exit(EXIT_FAILURE);
			}

			cout << depth << " | " <<  gv;
			cout << " | " << instance.nodesExpanded << " | " << finish-start << endl;

		}
		else if(!strcmp(argv[1],"MTD"))
		{
			//El MTD aqui.

			int depth = atoi(argv[2]);
			int f = atoi(argv[3]);
			state_t iNode;
			bool color = false; // Empieza jugando el blanco, que es MAX.

			iNode = iNode.genInit(depth, color);

			OthelloTranspositionTableNode * node = new OthelloTranspositionTableNode(iNode);
			MTDF * instance = new MTDF();

			if (!gettimeofday(&t_p,NULL))
				start = (double) t_p.tv_sec + ((double) t_p.tv_usec)/1000000.0;
			else{
					cout << "\nProblemas generando el tiempo final.\n";
					exit(EXIT_FAILURE);
			}

			//iNode.print(cout,depth);
			instance->mtdf(node, f , depth, color);

			if (!gettimeofday(&t_p,NULL))
				finish = (double) t_p.tv_sec + ((double) t_p.tv_usec)/1000000.0;
			else
				{
					cout << "\nProblemas generando el tiempo final.\n";
					exit(EXIT_FAILURE);
			}

			cout << depth << " | " <<  instance->getGameValue();
			cout << " | " << instance->getNumberOfNodes() << " | " << finish-start << endl;
			delete (instance);
			delete (node);


		}
		else
			cout << "Algoritmo invalido." << endl;

	}
	else
		cout << "Error en los paramtros." << endl;

}
